﻿using System;
using System.Collections.Generic;
using System.Linq;
using ConsoleApplication3;
using NPOI.HSSF.Record.Chart;
using timetable;

namespace timetable
{

    class Program
    {
        private static readonly string[] Coursename =
        {
            "语文", "数学", "英语", "物理", "化学", "生物", "政治", "历史", "地理", "通技"
        };
        static void Main(string[] args)
        {
            
            CourseSchedule cs = new CourseSchedule("./IO/config.txt");
            DataIO excel = new DataIO();
            List<Student> students = excel.GetStuInfoFromExcel("./IO/info.xls");

            // 输出语数外等主要课程（不需要进行分组的）
            // 存储语数外的排课结果
            //EnergyTable mainCourseEnergyTable = cs.GenGroupLists();

            //excel.OutputToExcelMainCourse("./排课结果/非文理科分类课程表.xls", cs.BetterEnergyTable.CompareList);


            cs.GenGroupListsByExcelClassifiedCourse("./IO/ClassWithTypes.xls"); // 王聪聪分班方法
            cs.GetGroupedStudentList("./IO/AllClass.xls");
            excel.OutputClassfiedClass("./IO/Group班级.xls", cs.resultGroupLists);

            //// 重新读取分班数据，开始排课
            cs = new CourseSchedule("./IO/config.txt");
            cs.GetMixedGroupList("./IO/Group班级.xls",false);
           
            cs.ClassifyAlgorithm();

            // 教师处理
            foreach (string c in Coursename)
            {
                    cs.BetterEnergyTable.teacherAndClassroom.AllocateTeacherClass(cs.BetterEnergyTable, c);
                    cs.BetterEnergyTable.teacherAndClassroom.OutputTeachers(cs.BetterEnergyTable, cs.GroupListClasses,
                        cs.studentAmountEachClass, c);
                    cs.BetterEnergyTable.teacherAndClassroom.OutputTeacherConlictTable("./排课结果/冲突课程/", c);
             }
       
             cs.BetterEnergyTable.teacherAndClassroom.WriteToExcel("./排课结果/文理科教师班级对照表.xls");
                // 将教师数据传给输出模块
             excel.AllTeacherList = cs.BetterEnergyTable.teacherAndClassroom.AllTeacherList;

            // TODO 输出所有的课程表
            //resultGroupList==> Group[i]=>table[j][k]
            //delete resultGroupList

            //excel.OutPutCourseTableFile("./IO/Table",cs.BetterEnergyTable.CompareList);
            excel.OutputCourseTimePairs("./IO/table.txt",cs.BetterEnergyTable.CompareList);

            excel.OutputToExcelMainCourse("./排课结果/文理科分类课程表.xls", cs.BetterEnergyTable.CompareList);

            int flag = 0;
           while (flag != 3)
           {
               proc3 pro3 = new proc3();
                string[] courses =null;
               Console.WriteLine("请查看排课结果，输入1 重分混合班； 输入2 分半混班；输入其他则退出");
               pro3.ReadGroupClass("IO/Group班级.xls");
               string s = Console.ReadLine();
               if (int.Parse(s) == 1)
               {
                   //重分混合班
                   pro3.ReallotMixClass();
                   flag = 1;
               }
               else if (int.Parse(s) == 2)
               {
                   //半混班
                   courses=pro3.HalfMixClass();
                   flag = 2;
               }
               else
               {
                   flag = 3;
               }

                // Group[i]=>table[j][k]=>resultGroupList
                //cs.GetMixedGroupList("./IO/Group班级.xls")=>resultGroupList


                // 仅仅获取课程集合和分组信息，不对课程表的信息进行任何修改
                cs = new CourseSchedule("./IO/config.txt");
               cs.GetMixedGroupList("./IO/Group班级.xls", true);
               // 还原课程表信息
               //cs.RecoverCourseTable("./IO/Table");
               cs.GetCouseTimeTable("./IO/table.txt");
               cs.ManualProcessRetails(courses);

                excel.OutputToExcelMainCourse("./排课结果/文理科分类课程表1.xls", cs.BetterEnergyTable.CompareList);
                // 教师处理
                foreach (string c in Coursename)
               {
                   cs.BetterEnergyTable.teacherAndClassroom.AllocateTeacherClass(cs.BetterEnergyTable, c);
                   cs.BetterEnergyTable.teacherAndClassroom.OutputTeachers(cs.BetterEnergyTable, cs.GroupListClasses,
                       cs.studentAmountEachClass, c);
                   cs.BetterEnergyTable.teacherAndClassroom.OutputTeacherConlictTable("./排课结果/冲突课程/", c);
               }

               cs.BetterEnergyTable.teacherAndClassroom.WriteToExcel("./排课结果/文理科教师班级对照表.xls");
               Console.WriteLine("");

                //生成修改过后的班级课表
                excel.OutputCourseTimePairs("./IO/table.txt", cs.BetterEnergyTable.CompareList);
            } 

            //// 天女散花
            //// 存在的问题：需要设置交换的条件
            //for (int i = 0; i < 5; i++)
            //{
            //    excel.StoreTeacherTakeCoursePosition(cs.BetterEnergyTable);
            //    excel.ExchangeSpecificPositionAllCourses(cs.BetterEnergyTable, i);
            //    excel.GenNewTeacherTakeCoursePosition(cs.BetterEnergyTable, i, 100);
            //    excel.StoreTeacherTakeCoursePosition(cs.BetterEnergyTable);
            //    excel.ExchangeSpecificPositionAllCourses(cs.BetterEnergyTable, i);
            //}

            //foreach (string s in Coursename)
            //{
            //    excel.OutputTeacherTable("./排课结果/文理科教师用课程表/", cs.BetterEnergyTable, s);
            //}
            //excel.OutputToExcelMainCourse("./排课结果/文理科分类课程表.xls", cs.BetterEnergyTable.CompareList);
            

            // 一致性检验
            //List<ChekedStudent> chekedStudents = new List<ChekedStudent>();
            //foreach (Student student in students)
            //{
            //    chekedStudents.Add(student);
            //    student.SetQuality();
            //}


            //List<string[]> groupAndGrouplistTable = new List<string[]>(); // 大类和分组之间的关系表

            //List<string[]> grouplistAndTeachClassTable = new List<string[]>(); // 大类和教学班的关系表
            //for (int i = 0; i < cs.BetterEnergyTable.CompareList.Count; i++)
            //{
            //    HashSet<string> className = new HashSet<string>();
            //    GroupList list = cs.BetterEnergyTable.CompareList[i];
            //    for (int j = 0; j < list.List.Count; j++)
            //    {
            //        Group group = list.List[j];
            //        var vl = group.Details.Values;
            //        string[] qualitys = new string[vl.Count];
            //        vl.CopyTo(qualitys,0);
            //        string quality = qualitys.Aggregate<string, string>(null, (current, s) => current + s);



            //        string[] groupAndGrouplist = {j.ToString(), i.ToString(), quality,group.count.ToString()};
            //        groupAndGrouplistTable.Add(groupAndGrouplist);

            //        // 找出每个大类的所有的教学班数据
            //        foreach (List<CourseCell> cells in group.Curriculum.table)
            //        {
            //            foreach (CourseCell cell in cells)
            //            {
            //                className.Add(cell.Class);
            //            }
            //        }
            //        // 跟学生对应的属性数据
            //        //var queryQuality =
            //        //    from student in chekedStudents
            //        //    where student.quality == quality
            //        //    select student;

            //        //foreach (ChekedStudent student in queryQuality)
            //        //{
            //        //    if (student.groupListNumber != 65535 || student.groupNumber != 65535)
            //        //    {
            //        //        if (student.groupListNumber != i || student.groupNumber != j)
            //        //        {
            //        //            throw new Exception("该学生已经被分组了");
            //        //        }

            //        //    }
            //        //    student.groupListNumber = i;
            //        //    student.groupNumber = j;
            //        //}
            //    }

            //    // 将每个大类的教学班数据存在列表中
            //    foreach (string s in className)
            //    {
            //        grouplistAndTeachClassTable.Add(new string[]{i.ToString(),s});
            //    }
            //}

            //// 按照教学班编号顺序排列    
            //// 输出教学班编号表
            //var sortTeachClass =
            //    from stringse in grouplistAndTeachClassTable
            //    where stringse[1] != null
            //    orderby stringse[1]
            //    select stringse[1];

            //foreach (string s in sortTeachClass)
            //{
            //    Console.WriteLine(s);
            //}

            //// 分班编号和人数的以及教师的表
            //foreach (string[] stringse in excel.courseAmount)
            //{
            //    Console.WriteLine(stringse[0]+"\t"+stringse[1]);
            //}
        }//main
    }
}
